/**
 * $Id: SGIPBindMessage.java,v 1.1 2006/09/07 02:08:40 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.sgip12msg;

import com.haoxi.gateway.util.TypeConvert;

public class SGIPBindMessage extends SGIPMessage {
    private String outStr = "";

    public SGIPBindMessage(int login_Type, String login_Name,
            String login_Password) throws IllegalArgumentException {
        // 构造参数进行效验
        if (null == login_Name) {
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:login_Name value is null.");
        }
        if (login_Name.length() > 16) {
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:login_Name length is larger than 16");
        }
        if (null == login_Password) {
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:login_Password value is null.");
        }
        if (login_Password.length() > 16) {
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:login_Password length is larger than 16");
        }
        if (login_Type < 0 || login_Type > 255) {
            throw new IllegalArgumentException(
                    "The parameter of login message is wrong:login_Type  less than 0 or larger than 255.");
        } else {
            int len = 61;
            buf = new byte[len];
            TypeConvert.int2byte(len, buf, 0);
            TypeConvert.int2byte(SGIPCommon.SGIP_BIND, buf, 4);
            buf[20] = (byte) login_Type;
            System.arraycopy(login_Name.getBytes(), 0, buf, 21, login_Name
                    .length());
            System.arraycopy(login_Password.getBytes(), 0, buf, 37,
                    login_Password.length());
            StringBuffer buffout = new StringBuffer(",login_Type=").append(
                    login_Type).append(",login_Name=").append(login_Name)
                    .append(",login_Password=").append(login_Password);
            outStr = buffout.toString();

        }
    }

    public SGIPBindMessage(byte buf[]) throws IllegalArgumentException {
        super.buf = new byte[53];
        if (buf.length != 53) {
            throw new IllegalArgumentException(
                    "The message that is sent by SMC is wrong");
        } else {
            System.arraycopy(buf, 0, super.buf, 0, 53);
            decodeSeq();
            StringBuffer buffout = new StringBuffer(",login_Type=").append(
                    buf[12]).append(",login_Name=").append(getLoginName())
                    .append(",login_Password=").append(getLoginPass());
            outStr = buffout.toString();
        }
    }

    public int getLoginType() {
        int tmpId = buf[12];
        return tmpId;
    }

    public String getLoginName() {
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, 13, tmpId, 0, 16);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public String getLoginPass() {
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, 29, tmpId, 0, 16);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public String toString() {
        String tmpbuf = new StringBuffer("SGIP_BIND: ")
                        .append("Src_node_Id=").append(getSrcNodeId())
                        .append(",Time_Stamp=").append(getTimeStamp())
                        .append(",Sequence_Id=").append(getSequenceId())
                        .append(outStr).toString();
        return tmpbuf.toString();
    }

    public int getCommandId() {
        return SGIPCommon.SGIP_BIND;
    }

}
